package uk.co.foocode.sample.petclinic.web.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class DwrExtStateManager {

	private Map preferenceMap = new HashMap();
	
	private Logger logger = Logger.getLogger(DwrExtStateManager.class);
	public void setPreference(String pageId, String settingName, String settingValue) {
		logger.info("setPreference("+pageId+"," +settingName+","+settingValue+")");
		getAllPreferences(pageId).put(settingName, settingValue);
	}
	public void clearPreference(String pageId, String settingName) {
		logger.info("clearPreference("+pageId+"," +settingName+")");
		getAllPreferences(pageId).remove(settingName);
	}
	public String getPreference(String pageId, String settingName) {
		String prefValue =(String) getAllPreferences(pageId).get(settingName); 
		logger.info("getPreference("+pageId+"," +settingName+"):"+prefValue);
		return prefValue;
	}
	
	public Map getAllPreferences(String pageId) {
		logger.info("getAllPreferences("+pageId+")");
		Map prefs = (Map) preferenceMap.get(pageId);
		if(prefs==null) {
			prefs = new HashMap();
			preferenceMap.put(pageId, prefs);
		}
		return prefs;
	}
}
